# -*- coding: utf-8 -*-
# @File    : items.py


from fastapi import APIRouter, Depends
from pydantic import BaseModel, Field

from ..ext import get_db, SessionLocal
from ..models.UserModels import User

router = APIRouter(
    prefix="/items",  # url前缀
    tags=["测试"],  # 标签
    responses={404: {"description": "Not found"}},  # 错误信息
)


@router.get("/")
async def read_root(db: SessionLocal = Depends(get_db)):
    dans = db.query(User).all()
    return {"dans": dans}


# 创建模型
class Product(BaseModel):
    category_id: int = Field(..., title="类别ID",  # 字段标题
                             description="产品的分类标识",   # 字段描述
                             gt=0)
    product_name: str = Field(..., title="产品名称", max_length=100,   # 最长长度
                              description="产品的名称")
    price: float = Field(..., title="价格", gt=0,    # gt必须大于0，lt必须小于0，ge大于等于，le小于等于，等于为eq
                         description="产品的售价，必须大于0")
    stock: int = Field(..., title="库存数量", ge=0, description="当前产品的库存量，非负数")
    is_featured: bool = Field(False,  # 默认值
                              title="是否特色产品", description="标记产品是否为特色或推荐产品")
    tags: list[str] = Field([], title="标签列表", description="与产品相关的标签数组")


@router.post("/products/", response_model=Product,    # 返回模型
             summary="创建产品",   # 接口描述
             description="详细描述接口",  # 详细描述
             tags=["产品"]    # 标签,
             )
async def create_product(product: Product):
    return {"product": product}

